package com.wontube.smr.dao.bo;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.WebsiteCategory;
import com.wontube.smr.dao.mapper.WebsiteCategoryMapper;
import com.wontube.smr.pub.DefaultDBHelper;


/**
 * @author yuechao
 *
 */
public final class WebsiteCategoryDao {
	private WebsiteCategoryDao() {
	}
	
	public static final List<String> listAllCategory(String countryCode) {
		ArrayList<String> listResult = new ArrayList<String>();
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("website_category.country_code = ", countryCode);
			ex.setOrderByClause("website_category.sort_index asc");
			List<WebsiteCategory> list = mapper.selectByExample(ex);
			for (WebsiteCategory category : list) {
				listResult.add(category.getCategoryName());
			}
		}finally{
			session.close();
		}
		
		return listResult;
	}
	
	public static final List<WebsiteCategory> listAllWebsiteCategories() {	
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("website_category.sort_index asc");
			ex.setOrderByClause("website_category.country_code");
			return mapper.selectByExample(ex);			
		}finally{
			session.close();
		}
	}	
	
	public static final boolean updateWebsiteCategoryByPK(WebsiteCategory website){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			int aff = mapper.updateByPrimaryKey(website);
			return aff == 1;
		}finally{
			session.close();
		}
	}
	
	public static final WebsiteCategory getWebsiteCategoryByPK(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			return mapper.selectByPrimaryKey(pk);		
		}finally{
			session.close();
		}
	}
	
	public static boolean saveOrUpdateWebsiteCategory(WebsiteCategory website){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			WebsiteCategory temp = mapper.selectByPrimaryKey(website.getId());
			int aff = 0;
			if(temp != null){
			 aff = mapper.updateByPrimaryKey(website);
			}else{
			 aff = mapper.insert(website);
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean deleteWebsiteCategory(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			WebsiteCategoryMapper mapper = session.getMapper(WebsiteCategoryMapper.class);
			int aff = mapper.deleteByPrimaryKey(pk);
			return aff == 1;
		} catch (Exception e) {
			return false;
		}finally{
			session.close();
		}
	}	
}
